package com.woniu.teachmall.config;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 支付宝配置类
 * 该类用于配置支付宝客户端所需的参数
 */
@Configuration
public class AlipayConfig {

    /**
     * 支付宝网关地址
     * 沙箱环境使用：https://openapi.alipaydev.com/gateway.do
     * 正式环境使用：https://openapi.alipay.com/gateway.do
     */
    private static final String URL = "https://openapi-sandbox.dl.alipaydev.com/gateway.do";

    /**
     * 应用ID
     * 在支付宝开放平台创建应用后获取
     */
    private static final String APP_ID = "2021000148666408";

    /**
     * 商户私钥
     * 使用支付宝开放平台提供的工具生成
     * 注意：私钥必须妥善保管，避免泄露
     */
    private static final String PRIVATE_KEY = "MIIEvwIBADANBgkqhkiG9w0BAQEFAASCBKkwggSlAgEAAoIBAQDMlqjQpiZVOeK0e3KGHCmdrhQAWJ1ODi6RZoxw6zpc4CcRineSd2UZ0suldxLIzYNgmGNkjDaSHf/k7eF9LknKw6kS3OZB6mENvY2yYEOCjEMT+eLJbUVKn04biOfT3qeOLxY0aUotsUUM5PV5V6obv+3j8O4kyLcVVd9+oKoiifUfkM/HhSxbf6LlH5z31CsAxZ5j11B819CJOmX14sWJpKCjPUvJOyUNYhG08WRQ/6h7NvLjj6djAfNPkBfuV8mplFwSENYHBk06NfbyYghLNOZET8v3LgwvT1/l0p0LxXhTJ3oqUrgQ0A9OjgOh0KP8YbuoBikNMUk+Swap1YI1AgMBAAECggEBAIproNpSn/rnyEgegdTU9x81KVk2m2+1TrKXtIuty3YNPsGycsZJO8EmCxzvnGj6C1puoEjmSQHBhGIqxHaCu/CRQzJ8/Nk6L+IxDs7SLLsGFVhNqLBp2wjrEdi1X0zNB4a+uwJI/qQ9nR9lReSMeI5/4/9mAmom1yB7UlSFdbcZdGBnKiqc0kqjkaUseE1P862tUkVeGGYzVn1ocMlFh8BV5OQmtFOMf1ouNK9pJymAGz+4SBt92wewHYjdA28RhmUxhc0T8dsvL18ys9vRr/sL0szN4naBBbxRfrkTnDv0xqbfh1T8Wa59CW3ijhnrGfvYiHTJkihfyhcxrom4/oECgYEA6SIAP4dxLS+m3x1gyj27OUjJO0J0sxnWNkAHcVvY+VNXrUhRiatBt8GOXmuvNu2PqOoJRNMOqUHik3N0lLv8ZatxB0VSdNoXLEUraZQdWpbliGbCh6+lxtDfADdoVA54HEJom9lnWksxIKXDMYay+dxdsxJp1bWhxY80OTb1kqECgYEA4KfoJ6AXJ8drKQHmjCRkBx97BC6n/tukzV7rS2DN6lyde6SF5ojTCL355HR5Py8lWuABgiQ28IehDOPM4GWXOUy+bFK0ylCaJu40fXoKh5V4LhM3vkUZUM9U2IQcfHq/VF2csbsK9GbhXiCnmlN1KtagaT9KtO/BJtdlDqhJmxUCgYAUz14qTZLCr9A7dknfg6cks+SSj5oPKq0+ZnTDn6k8iD35T81HdqwFFRf6Qa6APrBmKY3uQNC8Nw9cjh/H7wfWo3kRZxS3LYiIARyg7gG4xmNyy4ltxzYLCzX8tPVxG2GJVDw2PS/GlZOrEGDeGOH8/7i1dV1RIEdydRf6S3ecQQKBgQDgXHALGuPlM1C/HInODg3j2zm1HI9khKFbBV/B4QTk+OfCcrXTXXQ9N9WYpgXs5z3MOwjmmxpPL3TU9bBHrs3PSOioMBUkNEkvdhfv9HENpWXnxgOWv1WgEd/veEkmvEPWksMJ8P3oxdJvefdIte6/AKglVDNc7lCdZOIlOEaCXQKBgQC3votHSBUxM+qimCOqFT8ua9vqCu/5cXJPciugIcfZs4/J4wbDNKCshuBaLnA8ltSdYuePV4CbJaKFfmS3cujM3TiwjmMGA0u9yykBLjroGbKkK5mKx5QvyKg01DEc/dcoxAzJfZbFWKR5wbIknoHjRWrgL0r7DaJjpaQ1pOYpPg==";

    /**
     * 支付宝公钥
     * 在支付宝开放平台获取
     * 用于验证支付宝的异步通知
     */
    private static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzJao0KYmVTnitHtyhhwpna4UAFidTg4ukWaMcOs6XOAnEYp3kndlGdLLpXcSyM2DYJhjZIw2kh3/5O3hfS5JysOpEtzmQephDb2NsmBDgoxDE/niyW1FSp9OG4jn096nji8WNGlKLbFFDOT1eVeqG7/t4/DuJMi3FVXffqCqIon1H5DPx4UsW3+i5R+c99QrAMWeY9dQfNfQiTpl9eLFiaSgoz1LyTslDWIRtPFkUP+oezby44+nYwHzT5AX7lfJqZRcEhDWBwZNOjX28mIISzTmRE/L9y4ML09f5dKdC8V4Uyd6KlK4ENAPTo4DodCj/GG7qAYpDTFJPksGqdWCNQIDAQAB";

    /**
     * 支付宝异步通知地址
     * 支付宝服务器主动通知商户服务器里指定的页面http/https路径
     * 推荐使用https协议，并且必须是外网地址，支付宝服务器才可返回支付状态
     */
    private static final String NOTIFY_URL = "http://localhost:8080/alipay/notify";

    /**
     * 支付宝同步返回地址
     * 用户支付完成后，支付宝页面跳转回商户指定页面的地址
     */
    private static final String RETURN_URL = "http://localhost:8080/alipay/return";

    /**
     * 创建支付宝客户端
     * @return 返回AlipayClient实例，用于后续的支付操作
     */
    @Bean
    public AlipayClient alipayClient() {
        return new DefaultAlipayClient(
                URL,                // 支付宝网关URL
                APP_ID,            // 应用ID
                PRIVATE_KEY,       // 商户私钥
                "json",            // 请求格式
                "UTF-8",           // 编码格式
                ALIPAY_PUBLIC_KEY, // 支付宝公钥
                "RSA2"             // 签名方式
        );
    }
}
